package com.zhuss.car.processor;

import java.util.*;

import com.zhuss.car.processor.cache.DriverCache;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.ExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.car.driver.CreateDriverBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.car.db.dao.ext.DriverDao;
import com.zhuss.car.db.entity.Driver;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.JsonUtil;


/**
 * @description: 司机创建
 * @author: zhushanshuo
 * @create: Mon Dec 21 13:31:45 CST 2020
 **/
@Component("driverCreate")
public class DriverCreate extends AbstractProcessor<Object, CreateDriverBo> implements Processor<Object, CreateDriverBo> {

    @Autowired
    private DriverDao driverDao;

    @Autowired
	@Qualifier("driverRead")
	private DriverRead driverRead;

	@Autowired
	@Qualifier("driverCache")
	private DriverCache driverCache;


    /**
     * 校验
     */
    @Override
    public void check(CreateDriverBo bo) throws DzcxException {
    	String name = bo.getName();
    	if (name == null || "".equals(name.trim())) {
    		 throw new DzcxException(ExceptionCode.驾驶员Name为空);
    	}
		if (bo.getPhone() == null || "".equals(bo.getPhone())) {
			throw new DzcxException(ExceptionCode.驾驶员手机号为空);
		}

		String driverNo = bo.getDriverNo();
		if (driverNo == null || "".equals(driverNo.trim())) {
			throw new DzcxException(ExceptionCode.驾驶员driverNo为空);
		}

		if (bo.getCompanyId() == null || bo.getCompanyId() <=0l) {
			throw new DzcxException(ExceptionCode.所属公司为空);
		}

    	List<Driver> list = driverRead.list(driverNo);
    	if (!list.isEmpty()) {
    		 throw new DzcxException(ExceptionCode.驾驶员driverNo已存在);
    	}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(CreateDriverBo param) {
    	Driver record = new Driver();
    	record.setDriverNo(param.getDriverNo());
    	record.setName(param.getName());
    	record.setIdCard(param.getIdCard());
    	record.setCompanyId(param.getCompanyId());
		record.setStatus(param.getStatus() == null ? StartStopEnum.启用.key() : param.getStatus());
    	record.setAge(param.getAge());
    	record.setSex(param.getSex());
    	record.setPhone(param.getPhone());
    	record.setPic(param.getPic());
    	record.setSort(param.getSort());


    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	driverDao.create(record);
    	param.setTargetId(record.getDriverId());

		driverCache.clear(record.getDriverId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(CreateDriverBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.车辆.key());
    	record.setAction(LogActionEnum.司机创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
